# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.itemDao import ItemDao

from . import app

CORS(app, supports_credentials=True)

"""项目表 zzh_item API"""


@app.route('/add-item', methods=['POST'])
def add_item():
    if not request.json:
        abort(400)
    item_info = {
        "industry_name": request.json['industry_name'],
        "depart_name": request.json['depart_name'],
        "handler_name": request.json['handler_name'],
        "handler_phone": request.json['handler_phone'],
        "item_status": request.json['item_status'],
        "item_name": request.json['item_name'],
        "item_money": request.json['item_money'],
        "item_level": request.json['item_level'],
        "item_content": request.json['item_content'],
        "item_province": request.json['item_province'],
        "item_town": request.json['item_town'],
        "item_country": request.json['item_country'],
        "item_url": request.json['item_url'],
        "item_deadline": request.json['item_deadline'],
        "item_everyPrice": request.json['item_everyPrice'],
        "classfy_pid": request.json['classfy_pid'],
        "classfy_subname": request.json['classfy_subname']

    }

    result = ItemDao().add_item(item_info)
    if result:
        return jsonify({"itemInfo": item_info, "message": {"code": 1, "info": "添加成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "添加失败！"}})


@app.route('/update-item/<int:item_id>', methods=['POST'])
def update_item(item_id):
    if not request.json:
        abort(400)
    item_info = {
        "industry_name": request.json['industry_name'],
        "depart_name": request.json['depart_name'],
        "handler_name": request.json['handler_name'],
        "handler_phone": request.json['handler_phone'],
        "item_status": request.json['item_status'],
        "item_name": request.json['item_name'],
        "item_money": request.json['item_money'],
        "item_level": request.json['item_level'],
        "item_content": request.json['item_content'],
        "item_province": request.json['item_province'],
        "item_town": request.json['item_town'],
        "item_country": request.json['item_country'],
        "item_url": request.json['item_url'],
        "item_deadline": request.json['item_deadline'],
        "item_everyPrice": request.json['item_everyPrice'],
        "classfy_pid": request.json['classfy_pid'],
        "classfy_subname": request.json['classfy_subname']

    }

    result = ItemDao().update_item(item_id, item_info)
    return jsonify({"itemInfo": item_info, "message": {"code": 1, "info": "更新成功！"}})


@app.route('/query-item', methods=['GET'])
def query_item():
    query_result = ItemDao().query_item()

    return jsonify({"itemInfo": query_result, "message": {"code": 1, "info": "OK"}})


@app.route('/query-items-ifcondition', methods=['POST'])
def query_items_ifcondition():
    """
    item表的条件查询
    :param queryType: 查询类型
    :param keyword: 查询关键词
    :param item_area: 查询的item区域
    :param item_level: 查询的item级别
    :param item_status: 查询的item状态
    :return:
    """
    if not request.json:
        abort(400)
    queryType = request.json['queryType']
    keyword = request.json['keyword']
    classfyCode = request.json.get('classfyCode','')
    industryCode = request.json.get('industryCode', '')
    departCode = request.json.get('departCode', '')

    item_area = request.json.get('item_area', -1)
    item_level = request.json.get('item_level', -1)
    item_status = request.json.get('item_status', -1)
    if not item_area:
        item_area = -1
    if not item_level:
        item_level = -1
    if not item_status:
        item_status = -1

    conItems = ItemDao().conditions(queryType=queryType, keyword=keyword, classfyCode=classfyCode, industryCode=industryCode,
                                    departCode=departCode,item_area=item_area,
                                    item_level=item_level, item_status=item_status)

    if conItems:
        return jsonify({"items": conItems, "message": {"code": 1, "info": "OK！"}})
    else:
        return jsonify({"message": {"code": 1, "info": "该条件下无记录！"}})


# ------------------随机获取某分类下的item数据--------------------
@app.route('/query-hot-list', methods=['POST'])
def query_hot_list():
    if not request.json:
        abort(400)
    classCode = request.json['classfyCode']
    keyword = request.json.get('keyword', '')
    itemList = ItemDao().query_hot_list(classCode, keyword)
    if itemList:
        return jsonify({"itemList": itemList, "message": {"code": 1, "info": "OK！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "无相关信息！"}})


# ------------新增接口 批量查询item信息 2017-3-24   --------
@app.route('/query-item-list', methods=['POST'])
def query_item_list():
    """
    :param itemIds: item id 组成的数组，example：[1,2,3]
    :return:
    """
    if not request.json:
        abort(400)
    itemIds = request.json['itemIds']
    itemList = ItemDao().query_item_list(itemIds)

    return jsonify({"itemList": itemList, "message": {"code": 1, "info": "OK！"}})


@app.route('/query-item-by-itemId', methods=['POST'])  # 根据指定的itemID查询响应的Item详情
def queryItemByItemId():
    if not request.json:
        abort(400)
    itemId = request.json.get('itemId')  # 按照传递的itemId进行查询 ，必须传递此参数
    userId = request.json.get('userId')
    if not userId:
        itemListJson = ItemDao().queryItemByItemId(itemId)  # 返回值的获取顺序要匹配
        if itemListJson:
            return jsonify({"itemListJson": itemListJson, "message": {"code": 1, "info": "查询item表成功"}})
        else:
            return jsonify({"message": {"code": 0, "info": "未查询到item信息"}})
    else:
        itemListJson = ItemDao().queryItemByItemId(itemId)
        collectionInfo = ItemDao().queryCollectionInfo(userId, itemId)
        declarInfo = ItemDao().queryDeclarInfo(userId, itemId)
        indentInfo = ItemDao().queryIndetnInfo(userId, itemId)

        return jsonify({"statusInfo": {"collectionStatusInfo": collectionInfo, "declarStatusInfo": declarInfo,
                                       "indentStatusInfo": indentInfo}, "itemListJson": itemListJson,
                        "message": {"code": 1, "info": "查询item表成功"}})
